package com.longcai.piaojia.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.activity.GoodsInfoActivity;
import com.longcai.piaojia.adapter.MyCollectionListAdapter;
import com.longcai.piaojia.conn.MineCollectionDeleteGet;
import com.longcai.piaojia.conn.MyCollectionGet;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.longcai.piaojia.widget.ScrollViewSwipeListView;
import com.module.pull.PullToRefreshScrollView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/5.
 */
public class FragmentMyCollection extends BaseFragment implements MyCollectionListAdapter.DeleteItemCallback {

//    @BoundView(R.id.fragment_mycollection_sv) private PullToRefreshScrollView fragment_mycollection_sv;
    @BoundView(R.id.fragment_mycollection_lv) private ScrollViewSwipeListView fragment_mycollection_lv;
//    private ScrollViewSwipeListView mListView;
    private List<MyCollectionGet.MyCollection> lists = new ArrayList<>();
    private MyCollectionListAdapter adapter;
    private int deletePos = -1;


    private MineCollectionDeleteGet mineCollectionDeleteGet = new MineCollectionDeleteGet(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            UtilToast.show(getActivity(), getString(R.string.delete_success_text));

            lists.remove(deletePos);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            deletePos = -1;

            UtilToast.show(getActivity(), toast);

        }
    });

    private MyCollectionGet myCollectionGet = new MyCollectionGet(new AsyCallBack<MyCollectionGet.MyCollectionEntity>(){
        @Override
        public void onSuccess(String toast, int type, MyCollectionGet.MyCollectionEntity myCollectionEntity) throws Exception {

            lists.addAll(myCollectionEntity.myCollectionList);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(getActivity(), toast);
        }
    });

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//        View view = BoundViewHelper.boundView(this, inflater.inflate(R.layout.fragment_mycollection, null));

        View view_content = BoundViewHelper.boundView(this, inflater.inflate(R.layout.fragment_mycollection_content, null));

        myCollectionGet.user_id = BaseApplication.BasePreferences.readUserId();
        myCollectionGet.type = "1";
        myCollectionGet.execute(getActivity());

//        fragment_mycollection_sv.setScrollLoadEnabled(false);
//        fragment_mycollection_sv.setPullLoadEnabled(true);
//        fragment_mycollection_sv.setPullRefreshEnabled(true);
//
//        fragment_mycollection_sv.getRefreshableView().addView(view_content);

        fragment_mycollection_lv.setAdapter(adapter = new MyCollectionListAdapter(getContext(), lists, this));

        fragment_mycollection_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(getActivity(), GoodsInfoActivity.class).putExtra("id", lists.get(position).aid).putExtra("isCollection", true));

            }
        });

        fragment_mycollection_lv.setDivider(getResources().getDrawable(R.color.background_bg_color));

        fragment_mycollection_lv.setDividerHeight(ScaleScreenHelperFactory.getInstance().getWidthHeight(20));


        setAppCallBack(new RefreshCollectionlist());

        return view_content;
    }

    @Override
    public void deleteItem(final int pos, final View view) {

        new WarnningDialog(getActivity(), getString(R.string.mycollection_delete_text)) {
            @Override
            protected void onConfirm() {

                fragment_mycollection_lv.deleteItem(view);

                deletePos = pos;

                mineCollectionDeleteGet.user_id = BaseApplication.BasePreferences.readUserId();
                mineCollectionDeleteGet.aid = lists.get(pos).aid;
                mineCollectionDeleteGet.type = "1";

                mineCollectionDeleteGet.execute(getActivity());

            }
        }.show();


    }

    public class RefreshCollectionlist implements AppCallBack {

        public void refresh () {

            lists.clear();

            myCollectionGet.user_id = BaseApplication.BasePreferences.readUserId();
            myCollectionGet.type = "1";
            myCollectionGet.execute(getActivity());

        }
    }



}
